/*
 * @(#)NoticeBO.java $version 2011. 10. 14
 * 
 * Copyright 2011 NHN Corp. All rights Reserved.
 * NHN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */
package com.hangame.mbl.bo.notice;

import java.util.ArrayList;
import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.HttpException;
import org.apache.http.HttpStatus;

import com.hangame.mbl.model.notice.Notice;
import com.hangame.mbl.util.HttpClientUtil;
import com.nhncorp.lucy.common.data.DataHandlerFactory;

// TODO: Auto-generated Javadoc
/**
 * The Class NoticeBO.
 *
 * @author NHN
 */
public class NoticeBO {
	
	/** The Constant LOG. */
	private final static Log LOG = LogFactory.getLog(NoticeBO.class);
	
	/** The Constant NOTICE_URL. */
	private final static String NOTICE_URL = DataHandlerFactory.getDataHandler().get("url/notice_url");
	
	/** The Constant NOTICE_DETAIL_URL. */
	private final static String NOTICE_DETAIL_URL = DataHandlerFactory.getDataHandler().get("url/notice_detail_url");
	
	/** The Constant NOTICE_COUNT_URL. */
	private final static String NOTICE_COUNT_URL = DataHandlerFactory.getDataHandler().get("url/notice_count_url");

	/**
	 * Gets the notice list count.
	 * 
	 * @return the notice list count
	 */
	public int getNoticeListCount() {
		int noticeCount = 0;

		try {
			String[] response = HttpClientUtil.getResponseBodyGet(NOTICE_COUNT_URL, "UTF-8", 1000);
			int status = Integer.valueOf(response[0]);
			if (HttpStatus.SC_OK == status) {
				noticeCount = JSONObject.fromObject(response[1]).getInt("noticeCount");
			}
		} catch (Exception e) {
			LOG.error("error getNoticeListCount", e);
			return 0;
		}

		return noticeCount;
	}
	
	/**
	 * Gets the notice list from hsp.
	 * 
	 * @param offset the offset
	 * @param count the count
	 * @return the notice list from hsp
	 * @throws HttpException the http exception
	 */
	private List<Notice> getNoticeListFromHsp(int offset, int count) throws HttpException {
		List<Notice> noticeList = new ArrayList<Notice>();
		String url = NOTICE_URL + "&offset=" + offset / 15;
		String[] response = HttpClientUtil.getResponseBodyGet(url, "UTF-8", 1000);
		int status = Integer.valueOf(response[0]);
		if (HttpStatus.SC_OK == status) {
			JSONArray noticeArray = JSONObject.fromObject(response[1]).getJSONArray("noticeList");
			for (int i = 0; i < count; i++) {
				try {
					JSONObject noticeInfo = noticeArray.getJSONObject(i);
					Notice notice = new Notice();
					notice.setContents(noticeInfo.getString("contents"));
					notice.setNoticeNo(noticeInfo.getInt("noticeNo"));
					
					notice.setRegisterDate(formateRegisterDate(noticeInfo.getString("registerDate")));
					notice.setTitle(noticeInfo.getString("title"));
					noticeList.add(notice);
				} catch (IndexOutOfBoundsException e) {
					return noticeList;
				}
			}
		}
		return noticeList;
	}

	/**
	 * Gets the notice list.
	 * 
	 * @param offset the offset
	 * @param count the count
	 * @return the notice list
	 */
	public List<Notice> getNoticeList(int offset, int count) {
		List<Notice> result = new ArrayList<Notice>();

		try {
			//result = this.noticeDAO.selectNoticeList(offset, count);
			result = getNoticeListFromHsp(offset, count);
			if (result == null) {
				throw new Exception();
			}
		} catch (Exception e) {
			LOG.error(e, e);
			return new ArrayList<Notice>();
		}

		return result;
	}

	/**
	 * Gets the notice detail.
	 * 
	 * @param nId the n id
	 * @return the notice detail
	 */
	public Notice getNoticeDetail(int nId) {
		Notice notice = new Notice();

		try {
			String url = NOTICE_DETAIL_URL + "&noticeNo=" + nId;
			String[] response = HttpClientUtil.getResponseBodyGet(url, "UTF-8", 1000);
			int status = Integer.valueOf(response[0]);
			if (HttpStatus.SC_OK == status) {
				JSONObject noticeJSON = JSONObject.fromObject(response[1]).getJSONObject("notice");
				notice.setContents(noticeJSON.getString("contents"));
				notice.setNoticeNo(noticeJSON.getInt("noticeNo"));
				notice.setRegisterDate(formateRegisterDate(noticeJSON.getString("registerDate")));
				notice.setTitle(noticeJSON.getString("title"));
				notice.setPreNo(noticeJSON.getInt("preNo"));
				notice.setNextNo(noticeJSON.getInt("nextNo"));
			}
		} catch (Exception e) {
			LOG.error("error getNoticeDetail", e);
			return new Notice();
		}

		return notice;
	}
	
	/**
	 * Formate register date.
	 * 
	 * @param aRegisterDate the a register date
	 * @return the string
	 */
	private String formateRegisterDate(String aRegisterDate) {
		if (aRegisterDate != null && !"".equals(aRegisterDate)) {
			String[] dateArray = aRegisterDate.split(":");
			if (dateArray.length == 3) {
				return dateArray[0] + ":" + dateArray[1];
			} else {
				return aRegisterDate;
			}		
		} else {			
			return "";
		}
	}

}
